import router from './router'
import store from './store/store'


import {
  getToken
} from '@/utils/auth' // get token from cookie
import { supplayMenu } from './utils/common'

const whiteList = ['/login', '/auth-redirect']

router.beforeEach(

  (to, from, next) => {
    
    const hasToken = getToken()
    if (hasToken) {
      if (to.path === '/login') {
        // if is logged in, redirect to the home page
        next({
          path: '/'
        })

      } else {
        // 有用户信息
        if (store.token) {
          next()
        } else {
          if (store.state.roleMenList.length) {
            next()
          } else {
            console.log("====permissionelse====")
            console.log(supplayMenu)
            router.addRoutes(supplayMenu)
            store.commit('addMenuList', supplayMenu)
            next({
              ...to,
              replace: true
            })
          }

        }


      }
    } else {

      if (whiteList.indexOf(to.path) !== -1) {
        // in the free login whitelist, go directly
        next()
      } else {

        next(`/login?redirect=${to.path}`)


      }
    }
  }


)
